<?php

namespace App\Models;


class Archive
{

    /**
     * @var $time
     */
    protected $date;


    /**
     * Archive constructor.
     * @param $date
     */
    public function __construct($date)
    {
        $this->date = $date;
    }

    public function articles()
    {
        $start = date('Y-m-d', strtotime($this->date));
        $end = date('Y-m-d', strtotime('+1 Month', strtotime($this->date)));
        return Article::query()->select('id', 'title')
            ->where(['settings->status' => Article::STATUS_PUBLISHED])
            ->whereBetween('created_at', [$start, $end])
            ->orderBy('created_at', 'desc')
            ->get()->toArray();
    }


    public function toArray()
    {
        return [
            'time' => $this->date,
            'articles' => $this->articles(),
            'counts' => count($this->articles())
        ];
    }
}